package com.itany.netclass.service.proxy;

import com.itany.netclass.entity.Resource;
import com.itany.netclass.exception.NoQuanXianException;
import com.itany.netclass.exception.NullSringException;
import com.itany.netclass.exception.PDFException;
import com.itany.netclass.exception.ServiceException;
import com.itany.netclass.factory.ObjectFactory;
import com.itany.netclass.service.ResourceService;
import com.itany.netclass.tran.TransactionManager;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Author:sjy@itany.com
 * Date:2021/10/17 14:14
 * Version:1.0-SNAPSHOT
 */
public class ResourceServiceProxy implements ResourceService {

    @Override
    public Resource findMp4ByChapterId(String chapterId) throws ServiceException, NullSringException, PDFException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceServiceTarget");
        try {
            tran.begin();
            Resource mp4 = resourceService.findMp4ByChapterId(chapterId);
            tran.commit();
            return mp4;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (PDFException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void uploadPDF(String chapterId, HttpSession session, ServletOutputStream outputStream) throws ServiceException, IOException, NullSringException, PDFException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceServiceTarget");
        try {
            tran.begin();
            resourceService.uploadPDF(chapterId,session,outputStream);
            tran.commit();
        } catch (PDFException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        } catch (IOException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public List<Resource>  findResourceById(String resourceId) throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceServiceTarget");
        try {
            tran.begin();
            List<Resource> list = resourceService.findResourceById(resourceId);
            tran.commit();
            return  list;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }

    }

    @Override
    public List<Resource> findResourceByForm(String title, String nickname, String kaishitime, String jieshutime, String status, String fileType) throws ServiceException, NullSringException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceServiceTarget");
        try {
            tran.begin();
            List<Resource> list = resourceService.findResourceByForm(title, nickname, kaishitime, jieshutime, status, fileType);
            tran.commit();
            return list;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<Resource> findAllResource() throws NullSringException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceServiceTarget");
        try {
            tran.begin();
            List<Resource> resource = resourceService.findAllResource();
            tran.commit();
            return resource;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NullSringException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void yongResource(String resourceId, String status) throws Exception {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ResourceService resourceService = (ResourceService) ObjectFactory.getObject("ResourceServiceTarget");
        try {
            tran.begin();
            resourceService.yongResource(resourceId,status);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (NoQuanXianException e) {
            tran.rollback();
            throw e;
        }catch (Exception e){
            tran.rollback();
            throw e;
        }

    }
}
